import { Body, Controller, ForbiddenException, Get, Header, HttpCode, HttpException, HttpStatus, Param, ParseIntPipe, ParseUUIDPipe, Post, Query, Redirect, Req, Res, SetMetadata, UseFilters, UseGuards, UseInterceptors, UsePipes } from '@nestjs/common';
import { Request, Response } from 'express';
import { Observable, of } from 'rxjs';
import { CatsService } from 'src/cats/cats.service';
import { CreateCatDto } from 'src/cats/dto/create-cat.dto';
import { Cat } from 'src/cats/interfaces/cat.interface';
import { HttpExceptionFilter } from 'src/http-exception.filter';
import { AllExceptionsFilter } from 'src/any-exception.filter';
import { JoiValidationPipe } from 'src/joi-validate.pipe';
import { RolesGuard } from 'src/roles.guard';
import { Roles } from 'src/roles.decorator';
import { LoggingInterceptor } from 'src/logging.interceptor';
import { User } from 'src/user.decorator';

@Controller('cats')
// @UseGuards(RolesGuard)
@UseInterceptors(LoggingInterceptor)
export class CatsController {
    constructor(private catService: CatsService) {}
    
    @Post()
    @HttpCode(204)
    @Header('Cache-Control', 'none')
    async create(@Body() createCatDto: CreateCatDto) {
        // return 'This action adds a new cat';
        this.catService.create(createCatDto);
    }

    @Get('ab*cd')
    @Redirect('https://nestjs.com', 301)
    findAll(@Req() request: Request): string {
        return 'This action returns all cats 3';
    }

    @Get('docs')
    @Redirect('https://docs.nestjs.com', 302)
    getDocs(@Query('version') version) {
        if (version && version === '5') {
            return {url: 'https://docs.nestjs.com/v5/'};
        }
    }

    // @Get(':id')
    // findOne(@Param() params): string {
    //     console.log(params.id);
    //     return `This action returns a #${params.id} cat`;
    // }

    // @Get(':od')
    // findOne2(@Param('od') id): string {
    //     return `This action returns a #${id} cat`;
    // }

    @Get('asyncdemo')
    async findAll2(): Promise<any[]> {
        return [];
    }

    @Get('asyncdemo2')
    findAll3(): Observable<any[]> {
        return of([]);
    }

    @Get('asyncdemo3')
    findAll4(@Res() res: Response) {
        res.status(HttpStatus.OK).json([]);
    }

    @Get('asyncdemo5')
    // @UseFilters(AllExceptionsFilter)
    // @UsePipes(new JoiValidationPipe())
    async findAll5(): Promise<Cat[]> {
        // throw new HttpException('Forbidden', HttpStatus.FORBIDDEN);
        // throw new HttpException({
        //     status: HttpStatus.FORBIDDEN,
        //     error: 'This is a custom message',
        // }, HttpStatus.FORBIDDEN);
        // throw new ForbiddenException();
        throw new ForbiddenException();
        return this.catService.findAll();
    }

    // @Get(':id')
    // @SetMetadata('roles', ['admin'])
    // @Roles('admin')
    // async findOne3(@Param('id', new ParseIntPipe()) id) {
    //     console.log(id);
    //     return await this.catService.findOne(id);
    // }

    // @Get('decoratordemo1')
    // async findOne6(@User() user: UserEntity) {
    //     console.log(user);
    // }


}
